package com.winning.ywypt.util;

import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;

import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: NetDataUtil
 * @Author: sujun@winning.com.cn
 * @Desc: TODO
 * @Date: 2018-12-25 9:26
 **/

public class NetDataUtil {
    /**
     * 获取默认的HttpClient实例
     *
     * @return
     */
    public static HttpClient getInstance() {
        return HttpClients.createDefault();
    }

    /**
     * 链接管理器管理HttpCilent实例
     *
     * @param httpClientConnectionManager
     * @return
     */

    public static HttpClient getInstance(HttpClientConnectionManager httpClientConnectionManager) {
        HttpClientBuilder custom = HttpClients.custom();
        if (Objects.isNull(httpClientConnectionManager)) {
            custom.setConnectionManager(httpClientConnectionManager());
        } else {
            custom.setConnectionManager(httpClientConnectionManager);
        }

        return custom.build();
    }

    /**
     * 默认HttpGet请求
     *
     * @return
     */
    public static HttpGet httpGet() {
        return new HttpGet();
    }

    /**
     * 自定义HttpGet请求
     *
     * @param requestConfig
     * @return
     */
    public static HttpGet httpGet(RequestConfig requestConfig) {
        HttpGet httpGet = new HttpGet();
        if (Objects.isNull(requestConfig)) {
            httpGet.setConfig(requestConfig());
        } else {
            httpGet.setConfig(requestConfig);
        }

        return httpGet;
    }

    /**
     * 默认HttpPost
     *
     * @return
     */
    public static HttpPost httpPost() {
        return new HttpPost();
    }

    /**
     * 自定义HttpPost请求
     *
     * @param requestConfig
     * @return
     */
    public static HttpPost httpPost(RequestConfig requestConfig) {
        HttpPost httpPost = new HttpPost();
        if (Objects.isNull(requestConfig)) {
            httpPost.setConfig(requestConfig());
        } else {
            httpPost.setConfig(requestConfig);
        }

        return httpPost;
    }

    /**
     * 表单的字符格式化
     *
     * @param charset
     * @param nameValuePairs
     * @return
     * @throws UnsupportedEncodingException
     */
    public static UrlEncodedFormEntity urlEncodedFormEntity(Charset charset, @NotNull NameValuePair... nameValuePairs) throws UnsupportedEncodingException {
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(Arrays.asList(nameValuePairs), charset == null ? Consts.UTF_8 : charset);
        return urlEncodedFormEntity;
    }

    /**
     * 获取参数实例
     *
     * @param name
     * @param value
     * @return
     */
    public static NameValuePair nameValuePair(@NotNull String name, @NotNull String value) {
        return new BasicNameValuePair(name, value);
    }

    /**
     * 设置超时
     *
     * @return
     */
    public static RequestConfig requestConfig() {
        return RequestConfig.custom()
                .setSocketTimeout(1000 * 60 * 20)
                .setConnectTimeout(1000 * 60 * 2)
                .build();
    }

    /**
     * 自定义链接池
     *
     * @return
     */
    private static HttpClientConnectionManager httpClientConnectionManager() {
        ConnectionSocketFactory connectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
        LayeredConnectionSocketFactory layeredConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", connectionSocketFactory)
                .register("https", layeredConnectionSocketFactory)
                .build();

        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
                new PoolingHttpClientConnectionManager(registry);
        poolingHttpClientConnectionManager.setMaxTotal(500);
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(500);
        return poolingHttpClientConnectionManager;
    }

    public static URL url(String url) {
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        try {
            return new URL(url);
        } catch (Exception e) {

        }
        return null;

    }

    /**
     * 获取当前日期 yyyy-mm-dd hh:mm:ss
     *
     * @return
     */
    public static String CurrentDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String Date(@NotNull Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        return simpleDateFormat.format(date);
    }

    /**
     * 将传入数据分块
     *
     * @param data
     * @param index
     * @param count
     * @return
     */
    public static List bacthProcessDate(List data, int index, int count) {
        List subdata = new ArrayList();
        int startIndex = index * count;
        int endIndex = (startIndex + count) > data.size() ? data.size() : (startIndex + count);
        for (int i = startIndex; i < endIndex; i++) {
            subdata.add(data.get(i));
        }

        return subdata;
    }


}
